package com.mxpio.erp.technology.controller;


import com.mxpio.erp.common.technology.entity.*;
import com.mxpio.erp.common.technology.service.ProdTecRoutProcService;
import com.mxpio.erp.common.technology.service.ProdTecRoutService;
import com.mxpio.erp.common.technology.vo.VersionVO;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.policy.CrudContext;
import com.mxpioframework.jpa.policy.impl.SmartCrudPolicyAdapter;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @author wupaa
 */
@Tag(name = "ProdTecRoutController", description = "产品工艺路线接口")
@RestController
@RequestMapping("/erp/tech/prodrout/")
public class ProdTecRoutController {

    @Autowired
    private ProdTecRoutService service;

    @Autowired
    private ProdTecRoutProcService lineService;

    @PostMapping("listByItem/{productItemCode}")
    @Operation(summary = "产品工艺路线列表", description = "获取产品工艺路线列表（无分页）", method = "POST")
    public Result<ProdTecRout> listByItem(@Parameter(description="产品物料编号") @PathVariable(name = "productItemCode", required = true) String productItemCode,
                                      @Parameter(description="版本号") @RequestBody VersionVO vo ){
        ProdTecRout tecRout = service.listRout(productItemCode,vo);
        return Result.OK(tecRout);
    }

    @GetMapping("history/{productItemCode}")
    @Operation(summary = "产品历史工艺路线列表", description = "获取产品历史工艺路线列表（无分页）", method = "GET")
    public Result<List<ProdTecRout>> history(@Parameter(description="父物料编号") @PathVariable(name = "productItemCode", required = true) String productItemCode){
        List<ProdTecRout> bomInfos = service.listHistory(productItemCode);
        return Result.OK(bomInfos);
    }

    @GetMapping("list")
    @Operation(summary = "产品工艺路线列表", description = "获取产品工艺路线列表（无分页）", method = "GET")
    public Result<List<ProdTecRout>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<ProdTecRout> list = service.list(ProdTecRout.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "产品工艺路线列表", description = "获取产品工艺路线列表（分页）", method = "GET")
    public Result<Page<ProdTecRout>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<ProdTecRout> listPage = service.listPage(ProdTecRout.class, page, criteria);
        return Result.OK(listPage);
    }

    @GetMapping("list/{routId}")
    @Operation(summary = "根据routId获取产品工艺路线", description = "根据routId获取产品工艺路线", method = "GET")
    public Result<ProdTecRout> getById(@Parameter(description="通知单号") @PathVariable(name = "routId", required = true) String routId) {
        ProdTecRout data = service.getById(ProdTecRout.class, routId);
        return Result.OK(data);
    }

    @DeleteMapping("remove/{routIds}")
    @Operation(summary = "根据routId删除产品工艺路线", description = "删除产品工艺路线", method = "DELETE")
    public Result<ProdTecRout> remove(@Parameter(description="业务单号，多个单号用英文逗号分隔") @PathVariable(name = "routIds", required = true) String routIds) {
        String[] routId = routIds.split(",");
        for(String key : routId){
            Criteria criteria = Criteria.create();
            criteria.addCriterion("routId", Operator.EQ,key);
            lineService.removeBatch(ProdTecRoutProc.class,criteria);
            service.delete(ProdTecRout.class, key);
        }
        return Result.OK();
    }

    @RequestMapping("save")
    @Operation(summary = "保存", description = "保存")
    public Result<ProdTecRout> save(@RequestBody ProdTecRout rout) {
        service.save(rout,new SmartCrudPolicyAdapter(){
            @Override
            public boolean beforeInsert(CrudContext context) {
                Object o = context.getEntity();
                if(o instanceof ProdTecRoutProc){
                    ((ProdTecRoutProc) o).setRoutId(rout.getRoutId());
                }
                return true;
            }
        });
        return Result.OK(rout);
    }
    @GetMapping("unrelatedprod/page")
    @Operation(summary = "产品工艺路线未关联产品物料", description = "获取产品工艺路线未关联产品物料列表(分页)", method = "GET")
    public Result<Page<Item>> getUnrelatedProdPage(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
        Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
        Page<Item> page = service.getUnrelatedProdPage(pageAble, criteria);
        return Result.OK(page);
    }
}
